package com.cmcc.pcap.packet.util.pfcpUtile;

import com.cmcc.packet.entity.PfcpEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class RecoveryTimeStampUtil implements PfcpTypeUtil{
    private static Logger logger = LogManager.getLogger(PfcpTypeUtil.class);
    Date RecoveryTimeStamp;

    @Override
    public PfcpEntity.InformationElement analysisIe(byte[] sub_payload) {
        PfcpEntity.InformationElement peie = new PfcpEntity.InformationElement();
        peie.setType(((sub_payload[0] & 0xff) << 8) + (sub_payload[1] & 0xff));
        peie.setLength(((sub_payload[2] & 0xff) << 8) + (sub_payload[3] & 0xff));
        Map<String, String> dataMap = new HashMap<>();
        if(sub_payload.length >= 4+4) {
            long timestamp = (long) (sub_payload[4] << 24 | sub_payload[5] << 16 | sub_payload[6] << 8 | sub_payload[7]);
            Date date = new Date(timestamp);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            dataMap.put("RecoveryTimeStamp", sdf.format(date));
        }else {
            logger.error("Inadequate TLV length: %d", sub_payload.length);
        }

        return peie;
    }
}
